/*
 * 独立验证程序：验证调度结果的V_stay计算是否正确
 */
#include "graph.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("=== 调度结果独立验证程序 ===\n");
    
    // 1. 加载图数据
    Graph* g = load_graph_from_csv("data/CSV-version/Conv_Case0_Nodes.csv",
                                  "data/CSV-version/Conv_Case0_Edges.csv");
    if (!g) {
        printf("❌ 图数据加载失败\n");
        return 1;
    }
    printf("✅ 图数据加载成功: %d 节点, %d 边\n", g->num_nodes, g->num_edges);
    
    // 2. 读取C语言计算的调度结果
    FILE* schedule_fp = fopen("result/Problem1/Conv_Case0_schedule.txt", "r");
    if (!schedule_fp) {
        printf("❌ 无法读取调度结果文件\n");
        free_graph(g);
        return 1;
    }
    
    int* schedule = malloc(g->num_nodes * sizeof(int));
    for (int i = 0; i < g->num_nodes; i++) {
        if (fscanf(schedule_fp, "%d", &schedule[i]) != 1) {
            printf("❌ 调度文件格式错误\n");
            free(schedule);
            fclose(schedule_fp);
            free_graph(g);
            return 1;
        }
    }
    fclose(schedule_fp);
    printf("✅ 调度序列读取成功: %d 个节点\n", g->num_nodes);
    
    // 3. 验证调度的完整性和正确性
    bool* node_scheduled = calloc(g->num_nodes, sizeof(bool));
    for (int i = 0; i < g->num_nodes; i++) {
        int node_id = schedule[i];
        if (node_id < 0 || node_id >= g->num_nodes) {
            printf("❌ 调度中包含无效节点ID: %d\n", node_id);
            free(schedule);
            free(node_scheduled);
            free_graph(g);
            return 1;
        }
        if (node_scheduled[node_id]) {
            printf("❌ 节点%d被重复调度\n", node_id);
        }
        node_scheduled[node_id] = true;
    }
    
    // 检查是否所有节点都被调度
    for (int i = 0; i < g->num_nodes; i++) {
        if (!node_scheduled[i]) {
            printf("❌ 节点%d未被调度\n", i);
        }
    }
    free(node_scheduled);
    printf("✅ 调度完整性检查通过\n");
    
    // 4. 重新计算V_stay峰值进行验证
    int* vstay_history = malloc(g->num_nodes * sizeof(int));
    int calculated_peak = calculate_max_vstay(schedule, g->num_nodes, g, vstay_history);
    
    printf("\n=== V_stay峰值验证结果 ===\n");
    printf("重新计算的V_stay峰值: %d\n", calculated_peak);
    printf("摘要文件中的V_stay峰值: 6215\n");
    printf("数值一致性: %s\n", (calculated_peak == 6215) ? "✅ 一致" : "❌ 不一致");
    
    // 5. L0约束验证
    bool l0_valid = is_l0_schedule_valid(schedule, g->num_nodes, g);
    printf("L0约束满足: %s\n", l0_valid ? "✅ 满足" : "❌ 违反");
    
    // 6. 详细的V_stay变化分析
    printf("\n=== V_stay变化分析 ===\n");
    int max_step = 0;
    for (int i = 0; i < g->num_nodes; i++) {
        if (vstay_history[i] == calculated_peak) {
            max_step = i;
            break;
        }
    }
    printf("峰值出现位置: 第%d步 (节点%d)\n", max_step + 1, schedule[max_step]);
    
    // 显示峰值附近的变化
    int start = (max_step > 5) ? max_step - 5 : 0;
    int end = (max_step + 5 < g->num_nodes) ? max_step + 5 : g->num_nodes - 1;
    printf("峰值附近的V_stay变化:\n");
    for (int i = start; i <= end; i++) {
        printf("  步骤%d (节点%d): V_stay=%d %s\n", 
               i + 1, schedule[i], vstay_history[i],
               (i == max_step) ? "← 峰值" : "");
    }
    
    // 清理
    free(schedule);
    free(vstay_history);
    free_graph(g);
    
    printf("\n=== 验证完成 ===\n");
    return 0;
}





